﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Lib.Helpers;
using SDT.Model;
using SDT.Lib.Common;

namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ProfessionController : Controller
    {
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        //
        // GET: /Profession/
		[Authorize()]
		public ActionResult Index()
        {
			string order = (Request["order"] == null || Request["order"] == "") ? "ID" : Request["order"].ToString();
			string orderDir = (Request["orderDir"] == null || Request["orderDir"] == "") ? "asc" : Request["orderDir"].ToString();

			var enquiryTypes = eDataContext.Professions.OrderBy(string.Format("{0} {1}", order, orderDir)).ToList();

			ViewData["orderDir"] = orderDir;
			ViewData["order"] = order;
			return View(enquiryTypes);
        }

        //
        // GET: /Profession/Details/5
		[Authorize()]
        public ActionResult Details(int id)
        {
			var item = eDataContext.Professions.Single(e => e.ID == id);
			return View(item);
        }

        //
        // GET: /Profession/Create
		[Authorize()]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Profession/Create

        [HttpPost]
		[ValidateInput(false)]
		[Authorize()]
        public ActionResult Create(Profession enquiryType)
        {
            try
            {
				var item = new Profession();
				item.Name = enquiryType.Name;
				item.Description = enquiryType.Description;
				item.Modified = DateTime.Now;
				eDataContext.Professions.InsertOnSubmit(item);
				eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Profession/Edit/5
		[Authorize()]
        public ActionResult Edit(int id)
        {
			var enquiryType = eDataContext.Professions.Single(e => e.ID == id);
			return View(enquiryType);
        }

        //
        // POST: /Profession/Edit/5

        [HttpPost]
		[ValidateInput(false)]
		[Authorize()]
        public ActionResult Edit(Profession enquiryType)
        {
            try
            {
				var item = eDataContext.Professions.Single(e => e.ID == enquiryType.ID);
				item.ID = enquiryType.ID;
				item.Name = enquiryType.Name;
				item.Description = enquiryType.Description;
				item.Modified = DateTime.Now;
				eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Profession/Delete/5
		[Authorize()]
        public ActionResult Delete(int id)
        {
			var item = eDataContext.Professions.Single(e => e.ID == id);
            return View(item);
        }

        //
        // POST: /Profession/Delete/5

        [HttpPost]
		[Authorize()]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
				var item = eDataContext.Professions.Single(e => e.ID == id);
				if (item != null)
				{
					eDataContext.Professions.DeleteOnSubmit(item);
					eDataContext.SubmitChanges();
				}
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
